package com.laborer.PTSS.service.impl;

import com.laborer.PTSS.dao.UserDao;
import com.laborer.PTSS.dao.impl.UserDaoImpl;
import com.laborer.PTSS.domain.Course;
import com.laborer.PTSS.domain.User;
import com.laborer.PTSS.service.UserService;
import com.laborer.PTSS.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by fei on 2019/11/20.
 */
public class UserServiceImpl implements UserService{

    private UserDao userDao = new UserDaoImpl();
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    /**
     * 注册用户
     * @param user
     * @return
     */
    @Override
    public boolean regist(User user) {
        // 1.根据用户名查询用户对象
        User u = userDao.findByUsername(user.getUsername());
        // 判断u是否为null
        if(u != null){
            // 用户名存在，注册失败
            return false;
        }
        userDao.save(user);
        return true;
    }

    /**
     * 登录方法
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        return userDao.findByUsernameAndPassword(user.getUsername(),user.getPassword());
    }

    @Override
    public User getUserByUsername(String username) {
        String sql = "SELECT * from tab_user WHERE username = '"+ username +"'";
        System.out.println(sql);
        return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class));
    }
}
